package com.erp.common.utils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.List;

@Slf4j
public class JsonUtils {

    private JsonUtils() {
    }

    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    static {
        MAPPER.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    /**
     * 对象转ObjectNode
     * @param data 对象
     * @return ObjectNode
     */
    public static ObjectNode objectToNode(Object data) {
        try {
            String jsonStr = MAPPER.writeValueAsString(data);
            return jsonToNode(jsonStr, ObjectNode.class);
        } catch (JsonProcessingException e) {
            log.error("object to objectNode Exception", e);
        }
        return null;
    }

    /**
     * 对象数组转ArrayNode
     * @param datas 对象数组
     * @return ArrayNode
     */
    public static ArrayNode objectsToNode(Collection datas) {
        try {
            String jsonStr = MAPPER.writeValueAsString(datas);
            return jsonToNode(jsonStr, ArrayNode.class);
        } catch (JsonProcessingException e) {
            log.error("object to arrayNode Exception", e);
        }
        return null;
    }

    public static <T> T jsonToNode(String json, Class<T> clazz) {
        try {
            return MAPPER.readValue(json, clazz);
        } catch (JsonProcessingException e) {
            log.error("json to node Exception", e);
        }
        return null;
    }

    /**
     * 将对象转换成json字符串。
     * <p>Title: pojoToJson</p>
     * <p>Description: </p>
     *
     * @param data
     * @return
     */
    public static String objectToJson(Object data) {
        try {
            return MAPPER.writeValueAsString(data);
        } catch (JsonProcessingException e) {
            log.error("object to json Exception", e);
        }
        return null;
    }

    /**
     * 将json结果集转化为对象
     *
     * @param jsonData json数据
     * @param clazz    对象中的object类型
     * @return
     */
    public static <T> T jsonToObject(String jsonData, Class<T> clazz) throws IllegalArgumentException {
        try {
            return MAPPER.readValue(jsonData, clazz);
        } catch (Exception e) {
            log.error("json to pojo Exception", e);
            throw new IllegalArgumentException("Param deserialize exception, please check! " + e.getMessage(), e);
        }
    }

    /**
     * 将json结果集转化为对象
     *
     * @param jsonData json数据
     * @param clazz    对象中的object类型
     * @return
     */
    public static <T> T jsonToPojo(String jsonData, Class<T> clazz) {
        try {
            return MAPPER.readValue(jsonData, clazz);
        } catch (Exception e) {
            log.error("json to pojo Exception", e);
        }
        return null;
    }

    /**
     * 将json数据转换成pojo对象list
     * <p>Title: jsonToList</p>
     * <p>Description: </p>
     *
     * @param jsonData
     * @param beanType
     * @return
     */
    public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
        JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
        try {
            return MAPPER.readValue(jsonData, javaType);
        } catch (Exception e) {
            log.error("json to list Exception", e);
        }

        return null;
    }

}